$(function() {
	_change("input[id='bank']:visible", "select[id='bank_t']:visible");
	
	$("input[id^='bankcode']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=BankCode.ajax_zjm',
				dataType: "json",
				data: {
					bank: $('#bank').val(),
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {						
						return {
							branch: item.branch,
							label: item.code + " [" + item.branch + "]",
							value: item.code + ":" + item.branch
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("input[id^='branch']:visible").val(ui.item.branch);
		},
		
		minLength: 1
	});

	$.extend({
		format: function(str, step, splitor) {
			str = str.toString();
			var len = str.length;
 
			if (len > step) {
				var i = 0, s = "", arr = [];
				while (i < len) {
					s = str.substr(i, step);					
					arr.push(s);					
					i += step;					
				}				 
				 
				str = arr.join(splitor);
			 }
			
			 return str;
		}
	});

	var s = $.format(2000000010, 4, ' ');
	
	var code = $("input[id='code']:visible").val();
	// $("input[id='code']:visible").val($.format(code.replace(/\s/g, ''), 4, ' '));
	var b = $.format(code.replace(/\s/g, ''), 4, ' ');
	$("#code_t").html(b);
	
	$("input[id='code']:visible").keyup(function() {
		var s = $(this).val();
		// alert(s);
		var a = s.replace(/\s/g, '');
		
		var b = $.format(a, 4, ' ');
		//$(this).val(b);
		$("#code_t").html(b);
	});
	
	$("input[id^='province']:visible").autocomplete({
		minLength: 0,
		source: function( request, response ) {
			var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
			
			response( $.map( provinces, function( item ) {
				if ( item && ( !request.term || matcher.test(item.zjm) ) )
					return {
					label: item.name + " [" + item.zjm + "]",
					value: item.name
				};
			}) );
		}
	});	

	$("input[id^='city']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=Zone.ajax_zjm2',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							zone: item.zone,
							label: item.zjm + " [" + item.city + ", " + item.zone + "]",
							value: item.city
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("input[id^='zone']:visible").val(ui.item.zone);
		},
		
		minLength: 1
	});
	
	$("input[id^='zone']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=Zone.ajax_zjm2',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							city: item.city,
							label: item.zjm + " [" + item.city + ", " + item.zone + "]",
							value: item.zone
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("input[id^='city']:visible").val(ui.item.city);
		},
		
		minLength: 1
	});
	
	$("select[id^='category']:visible").change(function() {
		var a = $(this).val();
		_category(a);
	});
	
	_category($("select[id^='category']:visible").val());
	
	$( document ).tooltip();
});

function _category(a) {
	if (a == '1') {
		// $("#label_1").show();
		$("#label_2").show();
		$("#label_3").show();
		$("#label_9").hide();
	} else if (a == '2') {
		// $("#label_1").hide();
		$("#label_2").hide();
		$("#label_3").hide();
		$("#label_9").show();
	} else {
		$("#label_9").hide();
	}
}

function bank_emp() {
	// alert("emp");
	
	$("input[id^='info_id']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'emp.php?do=Emp.ajax_zjm',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 30,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							name: item.name,
							label: item.name + " [" + item.zjm + ", " + item.dept_t + "]",
							value: item.id + ":" + item.name
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("#name").val(ui.item.name);
		},
		
		minLength: 1
	});
}

function bank_supplier() {
	// alert("company");

	$("input[id^='info_id']:visible").autocomplete(Test.supplier);
}

function bank_customer() {
	// alert("customer");

	$("input[id^='info_id']:visible").autocomplete(Test.customer_my);
}
